package com.extr.domain.exam;

import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.web.servlet.view.document.AbstractExcelView;

import com.extr.file.util.ExcelUtil;

public class ExamHistoryViewExcel extends AbstractExcelView {

	@Override
	protected void buildExcelDocument(Map<String, Object> model,
			HSSFWorkbook workbook, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// TODO Auto-generated method stub
		List<ExamHistory> hisList = (List<ExamHistory>) model.get("list");
		
		HSSFSheet sheet = workbook.createSheet("list");    
		HSSFCellStyle style = workbook.createCellStyle();   
	    style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平  
	    sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 5));
        sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, 5));
        sheet.setDefaultColumnWidth(15);
        sheet.setColumnWidth(0, 4000);
        sheet.setColumnWidth(3, 5000);
        
        HSSFFont font = (HSSFFont) workbook.createFont(); //创建字体样式  
        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 字体加粗  
        font.setFontName("Times New Roman");  //设置字体类型  
        font.setFontHeightInPoints((short) 15);    //设置字体大小  
        style.setFont(font);    //为标题样式设置字体样式 
	    
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      
        HSSFCell cell = getCell(sheet, 0, 0);    
        setText(cell, "考试记录表");    
        cell.setCellStyle(style);    

        cell = getCell(sheet, 1, 0);   
        cell.setCellType(HSSFCell.CELL_TYPE_STRING);
        cell.setCellValue(format.format(new Date()));    
        cell.setCellStyle(style);    
		
        HSSFCellStyle headerStyle = (HSSFCellStyle) workbook .createCellStyle();// 创建标题样式  
        headerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        HSSFFont headerFont = (HSSFFont) workbook.createFont();  
        headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 字体加粗  
        headerFont.setFontName("Times New Roman");  
        headerFont.setFontHeightInPoints((short) 12);  
        headerStyle.setFont(headerFont);  
        headerStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 下边框  
        headerStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左边框  
        headerStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上边框  
        headerStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右边框
        
        getCell(sheet, 2, 0).setCellValue("试卷名称");  
        getCell(sheet, 2, 0).setCellStyle(headerStyle);
        getCell(sheet, 2, 1).setCellValue("姓名");  
        getCell(sheet, 2, 1).setCellStyle(headerStyle);
        getCell(sheet, 2, 2).setCellValue("部门");    
        getCell(sheet, 2, 2).setCellStyle(headerStyle);
        getCell(sheet, 2, 3).setCellValue("提交日期");
        getCell(sheet, 2, 3).setCellStyle(headerStyle);
        getCell(sheet, 2, 4).setCellValue("总分");    
        getCell(sheet, 2, 4).setCellStyle(headerStyle);
        getCell(sheet, 2, 5).setCellValue("得分");
        getCell(sheet, 2, 5).setCellStyle(headerStyle);

        HSSFCellStyle cellStyle = (HSSFCellStyle) workbook .createCellStyle();// 创建标题样式  
        HSSFFont cellFont = (HSSFFont) workbook.createFont();  
        cellFont.setFontName("Times New Roman");  
        cellFont.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
        cellFont.setFontHeightInPoints((short) 11);  
        cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 设置居中 
        cellStyle.setFont(cellFont);  
        cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 下边框  
        cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左边框  
        cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上边框  
        cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右边框
        
        for (int i = 0; i < hisList.size(); i++) {    
        	HSSFRow sheetRow = sheet.createRow(3+i);
        	sheetRow.createCell(0).setCellValue(hisList.get(i).getPaperName());
        	sheetRow.getCell(0).setCellStyle(cellStyle);
        	sheetRow.createCell(1).setCellValue(hisList.get(i).getUserName());
        	sheetRow.getCell(1).setCellStyle(cellStyle);
        	sheetRow.createCell(2).setCellValue(hisList.get(i).getDepartment());
        	sheetRow.getCell(2).setCellStyle(cellStyle);
        	HSSFCell tCell = sheetRow.createCell(3);
        	tCell.setCellValue(format.format(hisList.get(i).getSubmitTime()));
        	tCell.setCellStyle(cellStyle);
        	sheetRow.createCell(4).setCellValue(hisList.get(i).getTotalPoint());
        	sheetRow.getCell(4).setCellStyle(cellStyle);
        	sheetRow.createCell(5).setCellValue(hisList.get(i).getPointGet());
        	sheetRow.getCell(5).setCellStyle(cellStyle);
        }    
        String filename = "考试记录.xls";//设置下载时客户端Excel的名称     
        filename = ExcelUtil.encodeFilename(filename, request);//处理中文文件名  
        response.setContentType("application/vnd.ms-excel");     
        response.setHeader("Content-disposition", "attachment;filename=" + filename);     
        OutputStream ouputStream = response.getOutputStream();     
        workbook.write(ouputStream);     
        ouputStream.flush();     
        ouputStream.close(); 
	}

}
